
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>枚举 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="object-common.html" />
    
    
    <link rel="prev" href="object-mgmr.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">Java</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="object-mgmr.html">
            
                <a href="object-mgmr.html">
            
                    
                    对象管理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="object-common.html">
            
                <a href="object-common.html">
            
                    
                    对象通用行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="concurrent.html">
            
                <a href="concurrent.html">
            
                    
                    线程和并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="concurrent/how-to.html">
            
                <a href="concurrent/how-to.html">
            
                    
                    线程和线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="concurrent/block-close.html">
            
                <a href="concurrent/block-close.html">
            
                    
                    线程阻塞和关闭
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="concurrent/communicate.html">
            
                <a href="concurrent/communicate.html">
            
                    
                    线程协作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="concurrent/volatile.html">
            
                <a href="concurrent/volatile.html">
            
                    
                    活性失败-volatile
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="concurrent/lock.html">
            
                <a href="concurrent/lock.html">
            
                    
                    安全性失败-锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="concurrent/thread-local.html">
            
                <a href="concurrent/thread-local.html">
            
                    
                    线程局部存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="concurrent/others.html">
            
                <a href="concurrent/others.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="nio.html">
            
                <a href="nio.html">
            
                    
                    nio
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="advance.html">
            
                <a href="advance.html">
            
                    
                    泛型，注解和反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="mem-gc.html">
            
                <a href="mem-gc.html">
            
                    
                    内存模型和GC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="classloader.html">
            
                <a href="classloader.html">
            
                    
                    ClassLoader
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Web</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="spring.html">
            
                <a href="spring.html">
            
                    
                    spring
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">GUI</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="gui-1.html">
            
                <a href="gui-1.html">
            
                    
                    没事写个小界面什么的
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >枚举</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x679A;&#x4E3E;">&#x679A;&#x4E3E;</h1>
<table>
<thead>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>row 1 col 1</td>
<td>row 1 col 2</td>
</tr>
<tr>
<td>row 2 col 1</td>
<td>row 2 col 2</td>
</tr>
</tbody>
</table>
<p><img src="http://note.youdao.com/favicon.ico" alt="image"></p>
<pre><code>[link](http://note.youdao.com/)
</code></pre><ul>
<li>[x] dsfsdf</li>
<li>[ ] sfsdfs</li>
</ul>
<pre><code class="lang-math">E = mc^2
</code></pre>
<p><del>sdfsdf</del>
++sfsdfsdf++
<em>sfsfsdf</em>
<strong>sfasfs</strong></p>
<h2 id="sdfasdf">sdfasdf</h2>
<pre><code>gantt
dateFormat YYYY-MM-DD
section S1
T1: 2014-01-01, 9d
section S2
T2: 2014-01-11, 9d
section S3
T3: 2014-01-02, 9d
</code></pre><p>&#x6CE8;&#x89E3;</p>
<p><a href="http://blog.csdn.net/lmj623565791/article/details/43452969" target="_blank">http://blog.csdn.net/lmj623565791/article/details/43452969</a></p>
<h2 id="1-&#x57FA;&#x672C;&#x4F7F;&#x7528;">1 &#x57FA;&#x672C;&#x4F7F;&#x7528;</h2>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.cowthan.enum2;

<span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> Planet {

    MERCURY(<span class="hljs-number">3.302e+23</span>, <span class="hljs-number">2.439e6</span>),  <span class="hljs-comment">//&#x6C34;&#x661F;</span>
    VENUS(<span class="hljs-number">4.869e+24</span>, <span class="hljs-number">6.052e6</span>),  <span class="hljs-comment">//&#x91D1;&#x661F;</span>
    EARTH(<span class="hljs-number">5.975e+24</span>, <span class="hljs-number">6.378e6</span>),  <span class="hljs-comment">//</span>
    MARS(<span class="hljs-number">6.419e+23</span>, <span class="hljs-number">3.393e6</span>),   <span class="hljs-comment">//&#x706B;&#x661F;</span>
    JUPITER(<span class="hljs-number">1.899e+27</span>, <span class="hljs-number">7.149e7</span>), <span class="hljs-comment">//&#x6728;&#x661F;</span>
    SATURN(<span class="hljs-number">5.685e+26</span>, <span class="hljs-number">6.027e7</span>), <span class="hljs-comment">//&#x571F;&#x661F;</span>
    URANUS(<span class="hljs-number">8.683e+25</span>,  <span class="hljs-number">2.556e7</span>), <span class="hljs-comment">//&#x5929;&#x738B;&#x661F;</span>
    NEPTUNE(<span class="hljs-number">1.024e+26</span>, <span class="hljs-number">2.477e7</span>); <span class="hljs-comment">//&#x6D77;&#x738B;&#x661F;</span>

    <span class="hljs-comment">/*

     &#x679A;&#x4E3E;&#x5929;&#x751F;&#x4E0D;&#x53EF;&#x53D8;&#xFF0C;&#x56E0;&#x6B64;&#x6240;&#x6709;&#x57DF;&#x90FD;&#x5E94;&#x8BE5;&#x662F;final&#x7684;

     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">double</span> mass;   <span class="hljs-comment">//  kg</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">double</span> radius; <span class="hljs-comment">//  meter</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">double</span> surfaceGravity; <span class="hljs-comment">//    m/s^2</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> fuck = <span class="hljs-keyword">false</span>;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">double</span> G = <span class="hljs-number">6.67300E-11</span>;

    Planet(<span class="hljs-keyword">double</span> mass, <span class="hljs-keyword">double</span> radius) {
        <span class="hljs-keyword">this</span>.mass = mass;
        <span class="hljs-keyword">this</span>.radius = radius;
        surfaceGravity = G * mass / (radius * radius);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">mass</span><span class="hljs-params">()</span> </span>{<span class="hljs-keyword">return</span> mass;}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">radius</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> radius;}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">surfaceGravity</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> surfaceGravity;}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">surfaceWeight</span><span class="hljs-params">(<span class="hljs-keyword">double</span> mass)</span></span>{
        <span class="hljs-keyword">return</span> mass * surfaceGravity;
    }


    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">double</span> earthWeight = <span class="hljs-number">100</span>; <span class="hljs-comment">//kg</span>
        <span class="hljs-keyword">double</span> mass = earthWeight / Planet.EARTH.surfaceGravity();
        <span class="hljs-keyword">for</span>(Planet p: Planet.values()){
            System.out.printf(<span class="hljs-string">&quot;Weight on %s is %f%n&quot;</span>, p, p.surfaceWeight(mass));
        }
    }
}
</code></pre>
<pre><code>public enum Operation_1 {

    PLUS, MINUS, TIMES, DIVIDE;

    public double apply(double x, double y){

        switch(this){
        case PLUS: return x+y;
        case MINUS: return x-y;
        case TIMES: return x*y;
        case DIVIDE: return x/y;
        }
        throw new AssertionError(&quot;unknown op: &quot; + this);  ///&#x6CA1;&#x6709;&#x8FD9;&#x53E5;,&#x7F16;&#x8BD1;&#x4E0D;&#x4F1A;&#x901A;&#x8FC7;
    }

}
</code></pre><h2 id="2-&#x62BD;&#x8C61;&#x65B9;&#x6CD5;">2 &#x62BD;&#x8C61;&#x65B9;&#x6CD5;</h2>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.cowthan.enum2;

<span class="hljs-comment">/**
 * &#x7279;&#x5B9A;&#x4E8E;&#x5E38;&#x91CF;&#x7684;&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> Operation {

    PLUS(<span class="hljs-string">&quot;+&quot;</span>) {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span> </span>{
            <span class="hljs-keyword">return</span> x + y;
        }
    }, 
    MINUS(<span class="hljs-string">&quot;-&quot;</span>) {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span> </span>{
            <span class="hljs-keyword">return</span> x - y;
        }
    }, 
    TIMES(<span class="hljs-string">&quot;*&quot;</span>) {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span> </span>{
            <span class="hljs-keyword">return</span> x * y;
        }
    }, 
    DIVIDE(<span class="hljs-string">&quot;/&quot;</span>) {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span> </span>{
            <span class="hljs-keyword">return</span> x / y;
        }
    };

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String symbol;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Operation</span><span class="hljs-params">(String symbol)</span> </span>{
        <span class="hljs-keyword">this</span>.symbol = symbol;
    }

    <span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">double</span> <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">double</span> x, <span class="hljs-keyword">double</span> y)</span></span>;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> symbol;
    }
}
</code></pre>
<h2 id="3-enum&#x64CD;&#x4F5C;">3 enum&#x64CD;&#x4F5C;</h2>
<pre><code>enum&#x7684;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF1A;
Week[] values()&#xFF1A;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x5E38;&#x91CF;&#x7684;&#x6570;&#x7EC4;
Week valueOf(String)&#xFF1A;&#x6839;&#x636E;&#x5E38;&#x91CF;&#x540D;&#xFF0C;&#x83B7;&#x5F97;&#x679A;&#x4E3E;&#x5E38;&#x91CF;


enum&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#xFF1A;
name()&#xFF1A;&#x83B7;&#x53D6;&#x5E38;&#x91CF;&#x540D;
ordinal()&#xFF1A;&#x83B7;&#x53D6;&#x679A;&#x4E3E;&#x5E38;&#x91CF;&#x5728;&#x7C7B;&#x578B;&#x4E2D;&#x7684;&#x6570;&#x5B57;&#x4F4D;&#x7F6E;&#xFF0C;&#x4ECE;0&#x5F00;&#x59CB;


&#x7279;&#x522B;&#x6CE8;&#x610F;&#xFF1A;
ordinal()&#x662F;&#x5185;&#x7F6E;&#x7684;&#xFF0C;&#x8FD9;&#x4E2A;&#x662F;&#x5E8F;&#x6570;&#xFF0C;&#x662F;&#x4E3A;EnumSet&#x548C;EnumMap&#x8BBE;&#x8BA1;&#x7684;&#xFF0C;&#x7A0B;&#x5E8F;&#x5458;&#x4E0D;&#x5E94;&#x8BE5;&#x4F9D;&#x8D56;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x505A;&#x6709;&#x5173;&#x4E0B;&#x6807;&#x7684;&#x4E8B;

&#x4F60;&#x5E94;&#x8BE5;&#x7528;&#x5B9E;&#x4F8B;&#x57DF;&#x4EE3;&#x66FF;&#x5E8F;&#x6570;&#xFF0C;&#x4F8B;&#x5982;MONDAY(1)
</code></pre><h2 id="4-enumset&#x4EE3;&#x66FF;&#x4F4D;&#x57DF;&#xFF1A;-&#x679A;&#x4E3E;&#x7684;&#x96C6;&#x5408;">4 EnumSet&#x4EE3;&#x66FF;&#x4F4D;&#x57DF;&#xFF1A; &#x679A;&#x4E3E;&#x7684;&#x96C6;&#x5408;</h2>
<p>&#x4EC0;&#x4E48;&#x662F;&#x4F4D;&#x57DF;&#x5462;&#xFF0C;&#x5C31;&#x662F;&#x5E38;&#x89C1;&#x7684;flag&#x6A21;&#x5F0F;&#xFF0C;&#x6216;&#x8005;&#x6807;&#x5FD7;&#x4F4D;&#x6A21;&#x5F0F;</p>
<pre><code>public class Text{

    public static final int STYLE_BOLD          = 1 &lt;&lt; 0; //1   0001
    public static final int STYLE_ITALIC        = 1 &lt;&lt; 1; //2   0010
    public static final int STYLE_UNDERLINE     = 1 &lt;&lt; 2; //4   0100
    public static final int STYLE_STRIKETHROUGH = 1 &lt;&lt; 3; //8   1000

    public void applyStyles(int styles){
    }

}

text.applyStyles(STYLE_BOLD | STYLE_ITALIC);  

OR&#x8FD0;&#x7B97;&#x7B26;&#x53EF;&#x4EE5;&#x5C06;&#x51E0;&#x4E2A;&#x5E38;&#x91CF;&#x5408;&#x5E76;&#x5230;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x4E2D;&#xFF0C;
&#x8FD9;&#x5C31;&#x53EB;&#x4F4D;&#x57DF;&#xFF08;bit field&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x4F4D;&#x57DF;&#x662F;&#x6807;&#x5FD7;&#x4F4D;&#x7684;&#x96C6;&#x5408;


EnumSet&#x53EF;&#x4EE5;&#x7528;&#x5355;&#x4E2A;long&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x6027;&#x80FD;&#x6BD4;&#x5F97;&#x4E0A;&#x4F4D;&#x57DF;
</code></pre><p>&#x6539;&#x4E3A;&#x4F7F;&#x7528;&#x4F4D;&#x57DF;&#xFF1A;</p>
<pre><code>public class Text{
    public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }

    //&#x4EFB;&#x4F55;Set&#x90FD;&#x53EF;&#x4EE5;&#x4F20;&#x8FDB;&#x6765;&#xFF0C;&#x4F46;EnumSet&#x662F;&#x6700;&#x4F73;&#x7684;

    public void applyStyles(Set&lt;Style&gt; styles){
    }
}

&#x600E;&#x4E48;&#x8C03;&#x7528;&#xFF1A;text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC));
</code></pre><h2 id="5-enummap&#x4EE3;&#x66FF;&#x5E8F;&#x6570;&#x7D22;&#x5F15;">5 EnumMap&#x4EE3;&#x66FF;&#x5E8F;&#x6570;&#x7D22;&#x5F15;</h2>
<p>&#x907F;&#x514D;&#x4F7F;&#x7528;enum.ordinal()&#x4F5C;&#x4E3A;&#x6570;&#x7EC4;&#x4E0B;&#x6807;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x6839;&#x636E;enum&#x7684;&#x5E8F;&#x6570;&#x5FEB;&#x901F;&#x5B9A;&#x4F4D;&#x679A;&#x4E3E;&#xFF0C;&#x5E94;&#x8BE5;&#x7528;EnumMap</p>
<p>EnumMap&#xFF1A;&#x952E;&#x662F;&#x679A;&#x4E3E;&#x7684;Map</p>
<pre><code>Map&lt;Week, Set&lt;Course&gt;&gt; courses = new EnumMap&lt;Week, Set&lt;Course&gt;&gt;(Week.class);

for(Week w: Week.values()){
    courses.put(w, new HashSet&lt;Course&gt;&gt;);
}
</code></pre><p>&#x6307;&#x5B9A;&#x4E86;EnumMap&#x7684;key&#x7C7B;&#x578B;&#x4E4B;&#x540E;&#xFF0C;&#x7531;&#x4E8E;enum&#x7684;&#x5E38;&#x91CF;&#x4E2A;&#x6570;&#x662F;&#x56FA;&#x5B9A;&#x7684;&#xFF0C;<br>&#x6240;&#x4EE5;Enum&#x6700;&#x591A;&#x6709;&#x51E0;&#x4E2A;&#x952E;&#x4E5F;&#x662F;&#x56FA;&#x5B9A;&#x7684;&#xFF0C;<br>&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x5B8C;&#x7F8E;&#x54C8;&#x5E0C;</p>
<h2 id="6-&#x679A;&#x4E3E;&#x548C;string">6 &#x679A;&#x4E3E;&#x548C;String</h2>
<p>Enum.valueOf(s)&#x65B9;&#x6CD5;&#xFF0C;&#x6839;&#x636E;&#x5E38;&#x91CF;&#x540D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x76F4;&#x63A5;&#x5F97;&#x5230;&#x679A;&#x4E3E;&#x5E38;&#x91CF;<br>&#x5982;&#x679C;toString&#x88AB;&#x8986;&#x76D6;&#xFF08;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;&#x5E38;&#x91CF;&#x540D;&#xFF09;&#xFF0C;&#x5219;&#x4F60;&#x9700;&#x8981;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x6765;&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x679A;&#x4E3E;&#x5E38;&#x91CF;&#x7684;&#x6620;&#x5C04;&#xFF1A;</p>
<pre><code>public enum Week {

    Monday;


    public String toString() {
        if(this == Week.Monday){
            return &quot;&#x5468;&#x4E00;&quot;;
        }
        return &quot;&quot;;
    }

    private static final Map&lt;String, Week&gt; stringToEnum  = new HashMap&lt;&gt;();
    static{
        for(Week w: Week.values()){
            stringToEnum.put(w.toString(), w);
        }
    }

    public static void main(String[] args) {

        Week monday = Week.valueOf(&quot;Monday&quot;);   ///&#x8F6C;&#x6362;&#x679A;&#x4E3E;&#x5E38;&#x91CF;name
        System.out.println(monday);

        Week monday2 = Week.stringToEnum.get(&quot;&#x5468;&#x4E00;&quot;);  ///&#x8F6C;&#x6362;&#x81EA;&#x5B9A;&#x4E49;&#x5B57;&#x7B26;&#x4E32;
        System.out.println(monday2);

    }

}
</code></pre><h2 id="7-&#x4F8B;&#x5B50;">7 &#x4F8B;&#x5B50;</h2>
<p>&#x7B56;&#x7565;&#x679A;&#x4E3E;</p>
<p> &#x7B56;&#x7565;&#x6A21;&#x5F0F;&#x7684;&#x679A;&#x4E3E;&#x7248;&#xFF0C;&#x666E;&#x901A;&#x7684;&#x7B56;&#x7565;&#x6A21;&#x5F0F;&#x662F;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x7C7B;&#x6765;&#x63D0;&#x4F9B;&#x5177;&#x4F53;&#x7B97;&#x6CD5;</p>
<p> &#x9700;&#x6C42;&#xFF1A;&#x7B97;&#x6BCF;&#x5929;&#x7684;&#x5DE5;&#x8D44;<br> &#x6BCF;&#x5929;&#x4E0A;&#x73ED;&#x6807;&#x51C6;&#x662F;8&#x5C0F;&#x65F6;&#xFF0C;&#x5DE5;&#x8D44;&#x56FA;&#x5B9A;<br> &#x8D85;&#x51FA;&#x90E8;&#x5206;&#x662F;&#x52A0;&#x73ED;&#xFF0C;&#x5DE5;&#x8D44;1.5&#x500D;<br> &#x5E73;&#x65F6;&#x8D85;&#x51FA;8&#x5C0F;&#x65F6;&#x7B97;&#x52A0;&#x73ED;<br> &#x5468;&#x672B;&#x5168;&#x7B97;&#x52A0;&#x73ED;  </p>
<p> &#x4F60;&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x9700;&#x6C42;&#xFF1A;<br> 1 &#x4E0A;&#x73ED;&#x7684;&#x8FD9;&#x4E00;&#x5929;&#x53EF;&#x80FD;&#x662F;&#x5DE5;&#x4F5C;&#x65E5;&#xFF0C;&#x5468;&#x672B;&#xFF0C;&#x4E5F;&#x6709;&#x53EF;&#x80FD;&#x662F;&#x5176;&#x4ED6;&#x8282;&#x5047;&#x65E5;&#xFF0C;&#x800C;&#x4E14;&#x52A0;&#x73ED;&#x5DE5;&#x8D44;&#x53EF;&#x80FD;&#x4E5F;&#x4E0D;&#x4E00;&#x6837;<br> 2 &#x6240;&#x4EE5;&#x5148;&#x5B9A;&#x4E49;&#x4E2A;&#x5DE5;&#x4F5C;&#x65E5;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x770B;&#x505A;&#x85AA;&#x8D44;&#x7C7B;&#x578B;&#xFF0C;&#x4E3B;&#x8981;&#x5904;&#x7406;&#x52A0;&#x73ED;&#x5DE5;&#x8D44;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x8FD9;&#x91CC;&#x662F;PayType</p>
<pre><code>public enum PayRollDay {

    MONDAY(PayType.WEEKDAY),
    TUESDAY(PayType.WEEKDAY),
    WEDNESDAY(PayType.WEEKDAY),
    THURSDAY(PayType.WEEKDAY),
    FRIDAY(PayType.WEEKDAY),
    SATURDAY(PayType.WEEKEND),
    SUBDAY(PayType.WEEKEND);

    private final PayType payType;
    PayRollDay(PayType payType){
        this.payType = payType;
    }

    double pay(double hoursWorked, double payRate){
        return payType.pay(hoursWorked, payRate);
    }




    private enum PayType{
        WEEKDAY{
            double overtimePay(double hours, double payRate){
                return hours &lt;= HOURS_PER_SHIFT ? 0 : (hours - HOURS_PER_SHIFT) * payRate / 2;
            }
        },

        WEEKEND{
            double overtimePay(double hours, double payRate){
                return (hours) * payRate / 2;
            }
        };


        private static final int HOURS_PER_SHIFT = 8;
        abstract double overtimePay(double hrs, double parRate);

        double pay(double hoursWorked, double payRate){
            double basePay = hoursWorked * payRate;
            return basePay + overtimePay(hoursWorked, payRate);
        }

    }


}
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="object-mgmr.html" class="navigation navigation-prev " aria-label="Previous page: 对象管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="object-common.html" class="navigation navigation-next " aria-label="Next page: 对象通用行为">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"枚举","level":"1.3","depth":1,"next":{"title":"对象通用行为","level":"1.4","depth":1,"path":"object-common.md","ref":"object-common.md","articles":[]},"previous":{"title":"对象管理","level":"1.2","depth":1,"path":"object-mgmr.md","ref":"object-mgmr.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"enum.md","mtime":"2017-04-09T05:58:29.488Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T09:50:22.406Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

